import { Component } from '@angular/core'
import { IonicPage, NavController, NavParams } from 'ionic-angular'

import { HttpServicesProvider } from '../../../providers/http-services/http-services'

import { OrderDetailPage } from '../order-detail/order-detail'

@IonicPage()
@Component({
  selector: 'page-order',
  templateUrl: 'order.html'
})
export class OrderPage {
  customer: string
  orderOption: Array<{ number: string }> //订单数据
  count: number = 1
  infiniteFlag: Boolean = true // 下拉刷新功能是否启动标识
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    private httpService: HttpServicesProvider
  ) {
    this.customer = navParams.get('item')
    let data = new Object()
    data['companyCode'] = this.customer
    data['currentPage'] = this.count
    this.httpService.requestDataPost(
      'app/mesPartialMangement/orderFlowNums',
      data,
      {},
      (status, datas) => {
        if (status) {
          this.orderOption = datas.orderFlowNums
        }
      }
    )
  }

  itemSelected(item) {
    let customer = this.customer
    this.navCtrl.push(OrderDetailPage, { item, customer })
  }

  // 下拉刷新
  doRefresh(refresher) {
    this.count = 1
    let data = new Object()
    data['companyCode'] = this.customer
    data['currentPage'] = this.count
    this.httpService.requestDataPost(
      'app/mesPartialMangement/orderFlowNums',
      data,
      {},
      (status, datas) => {
        if (status) {
          this.orderOption = datas.orderFlowNums
          this.infiniteFlag = true
          refresher.complete()
        }
      }
    )
  }
  // 上拉加载,当列表过长时触发
  doInfinite(infiniteScroll) {
    this.count++
    let data = new Object()
    data['companyCode'] = this.customer
    data['currentPage'] = this.count
    this.httpService.requestDataPost(
      'app/mesPartialMangement/orderFlowNums',
      data,
      {},
      (status, datas) => {
        if (status) {
          if (datas.orderFlowNums.length) {
            this.orderOption.push(...datas.orderFlowNums)
          } else {
            this.infiniteFlag = false
          }
          infiniteScroll.complete()
        }
      }
    )
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad OrderPage')
  }
}
